﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;


namespace WCFMINERP.ComControls
{
	using System.Collections.Generic;

	public struct EnumItem {
		public EnumItem(int value, string description) : this() {
			Value = value;
			Description = description;
		}
		public int Value { get; set; }
		public string Description { get; set; }

		#region Overrides of ValueType

		/// <summary>
		///     返回该实例的完全限定类型名。
		/// </summary>
		/// <returns>
		///     包含完全限定类型名的 <see cref="T:System.String" />。
		/// </returns>
		public override string ToString() {
			return Description;
		}

		#region Overrides of ValueType

		/// <summary>
		///     返回此实例的哈希代码。
		/// </summary>
		/// <returns>
		///     一个 32 位有符号整数，它是该实例的哈希代码。
		/// </returns>
		public override int GetHashCode() {
			return Description.GetHashCode();
		}

		#endregion

		#endregion
	}

	public static class EnumExtension {
		private static readonly Dictionary<Type, EnumItem[]> Cache = new Dictionary<Type, EnumItem[]>();
        //public static bool HasItemState(this int val, Status.ItemState state) {
        //    return ((Status.ItemState) val).HasFlag(state);
        //}
		private static EnumItem[] getEnumItems(Type em) {
			EnumItem[] enumItems;
			if (Cache.TryGetValue(em, out enumItems)) {
				return enumItems;
			}
			FieldInfo[] fieldInfos = em.GetFields();
			object instance = em.Assembly.CreateInstance(em.FullName);
			EnumItem[] items = new EnumItem[fieldInfos.Length - 1];
			for (int i = 1; i < fieldInfos.Length; i++) {
				DescriptionAttribute attribute = fieldInfos[i].GetCustomAttributes(typeof (DescriptionAttribute), true).First() as DescriptionAttribute;
				int value = (int) fieldInfos[i].GetValue(instance);
				if (attribute != null) {
					items[i - 1] = new EnumItem(value, attribute.Description);
				}
			}
			Cache[em] = items;
			return items;
		}
		public static EnumItem ConvertEnumItem(this Type em, int v) {
			EnumItem[] enumItems = getEnumItems(em);
			return enumItems.FirstOrDefault(c => c.Value == v);
		}
		public static EnumItem[] ConvertEnumItem(this Type em) {
			return getEnumItems(em);
		}
        public static string GetEnumDescription<TEnum>(object value)
        {
            string result = string.Empty;
            Type enumType = typeof(TEnum);
            if (enumType.IsEnum)
            {
                var name = System.Enum.GetName(enumType, Convert.ToInt32(value));
                if (name != null)
                {
                    object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (objs != null && objs.Length > 0)
                    {
                        DescriptionAttribute attr = objs[0] as DescriptionAttribute;
                        result = attr.Description;
                    }
                    else
                    {
                        //TODO
                    }
                }
                else
                {
                    //TODO
                }
            }
            return result;
        }
        public static List<string> GetAllEnumString<T>()
        {
            List<string> list = new List<string>();
            Type item = typeof(T);
            foreach (string s in Enum.GetNames(item))
            {
                list.Add(s);
            }
            return list;
        }
        public static T StringToEnum<T>(string obj)
        {
            if (string.IsNullOrEmpty(obj))
            {
                return default(T);
            }
            try
            {
                return (T)Enum.Parse(typeof(T), obj, true);
            }
            catch (Exception)
            {
                return default(T);
            }
        }
	}
}
